package com.qk.manangement.controller;

import com.qk.common.response.PageResult;
import com.qk.common.response.Result;
import com.qk.entity.pojo.Course;
import com.qk.manangement.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 获取所有课程信息
     * @return
     */
    @GetMapping
    public Result list(String name, Integer subject, Integer target,
                                   @RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
                       ){
        PageResult<Course> list = courseService.list(name, subject, target, page, pageSize);
        return Result.success(list);
    }

    /**
     * 删除课程根据id
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return courseService.deleteCourse(id) ? Result.success() : Result.error("删除失败");
    }

    /**
     * 添加课程
     */
    @PostMapping
    public Result add(@RequestBody Course course) {
        return courseService.addCourse(course) ? Result.success() : Result.error("添加失败");
    }

    /**
     * 根据id查询课程
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Course course = courseService.getById(id);
        return course == null ? Result.error("课程不存在") : Result.success(course);
    }

    /**
     * 修改课程
     */
    @PutMapping
    public Result update(@RequestBody Course course) {
        return courseService.updateCourse(course) ? Result.success() : Result.error("修改失败");
    }

    /**
     *  查询所有课程信息
     */
    @GetMapping("/list")
    public Result list() {
        return Result.success(courseService.list());
    }

    /**
     * 根据学科查询课程信息
     */
    @GetMapping("/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject) {
        return Result.success(courseService.listBySubject(subject));
    }
}
